package marathon;

import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class MarathonTranslator {
	SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
	Date dateFirst;

	private class ExerciseSegment
	{
		public String description;
		public String distance;
		public String minutes;
	}
	private class Exercise {
		public int day;
		public String distance;
		public String minutes;
		Vector<ExerciseSegment> segments=new Vector<>();
	}

	private class Exercises {
		Vector<Exercise> el = new Vector<>();
	}

	public void translate(Document doc) throws ParseException {
		Exercises exercises = new Exercises();
		Elements el = doc.select("div.exercise");

		dateFirst = formatter.parse(el.first().attr("id"));

		for (Element element : el) {
			Exercise e = new Exercise();
			e.day = (int) getDay(element);

			e.distance = getDistance(element);
			e.minutes = getMinutes(element);
			e.segments.addAll(getSegments(element));
			
			exercises.el.add(e);
		}
	}

	private Vector<ExerciseSegment> getSegments(Element element) {
		Vector<ExerciseSegment> segments=new Vector<>();
		Elements moments=element.select("div.field-exercise-moment");
		
		for (Element elseg : moments) {
			ExerciseSegment segment=new ExerciseSegment();				
			segment.description = StringEscapeUtils.unescapeHtml4(elseg.select("div.field-ref-exercise-type").first().text());
			if( !segment.description.toLowerCase().contains("vila") )
			{
				Elements segdist = elseg.select("span.field-readable-distance");
				if( !segdist.isEmpty() )
				{
					segment.distance = segdist.first().text();
				}
				else
				{
					segment.distance = "";
				}

				Elements segtime = elseg.select("span.field-readable-time");
				if( !segtime.isEmpty() )
				{
					segment.minutes = segtime.first().text();
				}
				else
				{
					segment.minutes = "";
				}
			
				segments.add(segment);
			}
			else
			{
				segment.description = "";
			}
		}
		return segments;
	}

	private void printExercise(Exercise e) {
		PrintStream out=System.out;
		out.println("<exercise>");
		out.println("  <day>" + e.day + "</day");
		out.println("  <distance>" + e.distance + "</distance>");
		out.println("  <minutes>" + e.minutes + "</minutes>");
		
		for (ExerciseSegment segment : e.segments) {
			if( segment.description != null )
			{
				out.println("  <segment>");
				out.println("    <description>");
				
				out.println("      " + segment.description);
				out.println("    </description>");
				out.println("  </segment>");
			}
		}
		out.println("</exercise>");
	}

	private String getDistance(Element element) {
		element.select("div.distance > span").remove();
		return element.select("div.distance").text();
	}

	private String getMinutes(Element element) {
		element.select("div.time > span").remove();
		return element.select("div.time").text();
	}

	private long getDay(Element el) throws ParseException {
		Date date = formatter.parse(el.attr("id"));
		long diff = date.getTime() - dateFirst.getTime();
		return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
	}
}
